#!/bin/bash
set -e

cd $(dirname $0)/..

ARCH=${ARCH:-amd64}
SUFFIX=""
[ "${ARCH}" != "amd64" ] && SUFFIX="_${ARCH}"

export VERSION=${VERSION:-$(./scripts/version)}

TAG=${TAG:-${VERSION}${SUFFIX}}
REPO=${REPO:-longhornio}
IMAGE=${IMAGE:-${REPO}/longhorn-manager:${TAG}}

if [ ! -e ./bin/longhorn-manager ]; then
    ./scripts/build
fi

trap 'rm -rf ./package/bin' exit

# update base image to get latest changes
BASE_IMAGE=`grep FROM package/Dockerfile  | awk '{print $2}'`
docker pull ${BASE_IMAGE}

docker build -t ${IMAGE} -f package/Dockerfile .

echo Built ${IMAGE}

echo ${IMAGE} > ./bin/latest_image
